package mqtt

import (
	"fmt"
	"github.com/goccy/go-json"
	"strings"
	"time"
)

const (
	Host="123.60.187.31:8810"
	UserName="haibang"
	Password="Huawei@123"
)
var MqClient *Client
var topics []string
// 初始化
func init()  {
	topics =append(topics, "mqtt")
	MqClient =&Client{}
	MqClient.NewClient(UserName)
	go func() {
		for {
			time.Sleep(time.Second*10)
			if !MqClient.nativeClient.IsConnected() && !MqClient.nativeClient.IsConnectionOpen(){
				fmt.Println("尝试连接")
				MqClient.NewClient(UserName)
				err:= MqClient.Connect()
				if err!=nil{
					//记录日志
					fmt.Println("SubMqtt连接错误,原因:"+err.Error())
					continue
				}
			}
		}

	}()
}


// Message 数据响应
type Message struct {
	ClientID string `json:"clientId"`
	Type     string `json:"type"`
	Data     string `json:"data,omitempty"`
	Time     int64  `json:"time"`
	Topic    string `json:"topic"`
}

func decodeMessage(payload []byte) (*Message, error) {
	message := new(Message)
	decoder := json.NewDecoder(strings.NewReader(string(payload)))
	decoder.UseNumber()
	if err := decoder.Decode(&message); err != nil {
		return nil, err
	}
	return message, nil
}